package cn.com.huzo.webapp.controller.admin;

import huzofw.common.web.controller.BaseFormController;
import huzofw.model.entities.security.PrivilegeEntity;
import huzofw.model.entities.security.ResourceEntity;
import huzofw.service.security.PrivilegeManager;
import huzofw.service.security.ResourceManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统权限控制器
 *
 * @author 吴旭
 */
@Controller
public class PrivilegeController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "PrivilegeManager")
    private PrivilegeManager manager_privilege;

    @Resource(name = "ResourceManager")
    private ResourceManager manager_resource;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到系统权限编辑画面
     * <p>
     * @return
     */
    @RequestMapping("admin/privilege/list.html")
    public ModelAndView redirect() {
        ModelAndView view = new ModelAndView();
        view.setViewName("admin.privilege.list.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得全部系统资源数据。
     *
     * @return 系统操作列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/fetchAllResource.html",
            method = RequestMethod.POST)
    @ResponseBody
    public List<ResourceEntity> getAllResource() throws Exception {
        return manager_resource.fetchAllResource();
    }

    /**
     * 取得用户的权限
     * <p>
     * @param criterion
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/fetchPrevilegeByUserName.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchPrevilegeByUserName(@RequestBody final Map<String, Object> criterion) throws Exception {
        return manager_privilege.fetchAllPrivilegeByUserName(criterion.get("UserName").toString());
    }

    /**
     * 取得用户的该画面的操作权限
     * <p>
     * @param criterion
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/fetchResourceActionAccessPrivilegeByUserNameAndLinkUrl.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchActionAccessPrivilegeByUserNameAndLinkUrl(@RequestBody final Map<String, Object> criterion) throws Exception {
        return manager_privilege.fetchActionAccessPrivilegeByUserNameAndLinkUrl(
                criterion.get("UserName").toString(),
                criterion.get("LinkUrl").toString());
    }

    /**
     * 授权操作
     * <p>
     * @param criterion 用户系统权限
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/grantPrivilege.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Boolean grantPrivilege(@RequestBody Map<String, Object> criterion) throws Exception {
        return manager_privilege.grantPrivilege(criterion.get("UserName").toString(),
                (List<PrivilegeEntity>) criterion.get("ResourceAccessList"),
                (List<PrivilegeEntity>) criterion.get("ActionAccessList"));
    }

    /**
     * 授权操作
     * <p>
     * @param criterion 要复制权限给的用户名
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/copyPrivilege.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer copyPrivilege(@RequestBody Map<String, Object> criterion) throws Exception {
        return manager_privilege.copyPrivilege(criterion.get("UserName").toString(),
                (List<String>) criterion.get("OtherMemberList"));
    }

    /**
     * 回收权限。
     *
     * @param criterion 用户ID
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/privilege/revoke.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer revoke(@RequestBody final Map<String, Object> criterion) throws Exception {
        return manager_privilege.revoke(MapUtils.getString(criterion, "UserName"));
    }
    // </editor-fold>
}
